package com.itheima.test;

import com.itheima.dao.Userdao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class Mybatisthreetest {
    private InputStream in;
    private SqlSession session;
    private Userdao userDao;
    @Before
    public void invi() throws Exception{
        //读取文件
        in = Resources.getResourceAsStream("sqlMapConﬁg.xml");
        // InputStream in = MybatisTest.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3.使用工厂生产SqlSession对象
        session = factory.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        userDao = session.getMapper(Userdao.class);
    }
    @After
    public void destory() throws Exception{
        session.commit();
        session.close();
        in.close();

    }
    //利用注解版 查询所有
    @Test
    public void testselect(){
        List<User> users = userDao.findall();
        for(User user : users){
            System.out.println(user);
        }
    }
    //利用注解版 增加用户
    @Test
    public void testinsert(){
      User user=new User();
      user.setUsername("白眉鹰王");
      user.setSex("男");
      user.setAddress("宣武门");
      user.setBirthday(new Date());
      userDao.zhujieadd(user);
    }
    //利用注解版 修改用户
    @Test
    public void testupdate(){
        User user=new User();
        user.setId(43);
        user.setUsername("青衣斧王");
        user.setSex("男");
        user.setAddress("明教护法");
        user.setBirthday(new Date());
        userDao.zhujiexiugai(user);
    }
    //利用注解版 删除用户
    @Test
    public void testshanchu(){
      userDao.zhujieshanchu(53);
    }

    //利用注解版 模糊查询所有
    @Test
    public void testtwoselect(){

        int b=10;

        System.out.println("helloword");


        List<User> users = userDao.muhuchaxun("%王%");
        for(User user : users){
            System.out.println(user);
        }
    }


}
